package com.bank.controller;

import com.bank.entity.UserInfo;
import com.bank.mapper.UserInfoMapper;
import com.bank.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 用户信息控制器
 */
@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 查询所有客户信息
     */
    @GetMapping("/list")
    public List<UserInfo> list() {
        return userInfoMapper.findAll();
    }

    /**
     * 新增客户信息
     */
    @PostMapping
    public Result<String> add(@RequestBody UserInfo userInfo) {
        try {
            // 检查身份证号是否已存在
            UserInfo existingUser = userInfoMapper.findByPid(userInfo.getPid());
            if (existingUser != null) {
                return Result.error("身份证号 " + userInfo.getPid() + " 已存在，无法重复添加");
            }

            // 检查手机号是否已存在
            UserInfo existingUserByPhone = userInfoMapper.findByTelephone(userInfo.getTelephone());
            if (existingUserByPhone != null) {
                return Result.error("手机号 " + userInfo.getTelephone() + " 已存在，无法重复添加");
            }

            int result = userInfoMapper.insert(userInfo);
            if (result > 0) {
                return Result.success("客户添加成功");
            } else {
                return Result.error("客户添加失败");
            }
        } catch (Exception e) {
            return Result.error("添加客户时发生错误: " + e.getMessage());
        }
    }

    /**
     * 修改客户信息
     */
    @PutMapping
    public int update(@RequestBody UserInfo userInfo) {
        return userInfoMapper.update(userInfo);
    }

    /**
     * 删除客户信息
     */
    @DeleteMapping("/{customerID}")
    public int delete(@PathVariable Long customerID) {
        return userInfoMapper.deleteById(customerID);
    }

    /**
     * 根据pid获取用户信息
     */
    @GetMapping("/info")
    public Result<UserInfo> getUserInfo(@RequestParam String pid) {
        UserInfo user = userInfoMapper.findByPid(pid);
        if (user == null) {
            return Result.error("用户不存在");
        }
        return Result.success(user);
    }

    @GetMapping("/find")
    public Result<UserInfo> findById(@RequestParam Long customerID) {
        return Result.success(userInfoMapper.findById(customerID));
    }

    @GetMapping("/search")
    public Result<List<UserInfo>> search(
            @RequestParam(required = false) Long exactId,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long minId,
            @RequestParam(required = false) Long maxId) {

        if (exactId != null) {
            // 精确查询
            UserInfo user = userInfoMapper.findById(exactId);
            return Result.success(user != null ? Collections.singletonList(user) : Collections.emptyList());
        } else if (keyword != null && !keyword.isEmpty()) {
            // 模糊查询
            return Result.success(userInfoMapper.searchByKeyword("%" + keyword + "%"));
        } else if (minId != null && maxId != null) {
            // 范围查询
            return Result.success(userInfoMapper.findByIdRange(minId, maxId));
        } else {
            // 无查询条件，返回所有数据
            return Result.success(userInfoMapper.findAll());
        }
    }

    @GetMapping("/range")
    public Result<List<UserInfo>> range(@RequestParam Long minId, @RequestParam Long maxId) {
        return Result.success(userInfoMapper.findByIdRange(minId, maxId));
    }
}